/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.actions.movement;

import com.android1.amarena2d.actions.base.IntervalAction;
import com.android1.amarena2d.nodes.behavior.ActionTarget;

public class MoveIn extends ScreenMove {


    public static MoveIn $fromLeft(float duration) {
        return new MoveIn(DirectionLeft, duration);
    }

    public static MoveIn $fromRight(float duration) {
        return new MoveIn(DirectionRight, duration);
    }

    public static MoveIn $fromTop(float duration) {
        return new MoveIn(DirectionTop, duration);
    }

    public static MoveIn $fromBottom(float duration) {
        return new MoveIn(DirectionBottom, duration);
    }

    public MoveIn(int direction, float duration) {
        super(direction, duration);
    }

    /**
     * Will place the target at the given position on action start.
     *
     * @param x
     * @param y
     * @return
     */
    public MoveIn targetPosition(float x, float y) {
        this.fixTargetPosition = true;
        endPositionX = x;
        endPositionY = y;
        return this;
    }


    @Override
    public void start(ActionTarget target) {
        super.start(target);
        if (!fixTargetPosition) {
            endPositionX = target.getX();
            endPositionY = target.getY();
        }

        if (this.direction == DirectionLeft) {
            this.startPositionY = endPositionY;
            this.startPositionX = engine.getCamera().getLeft() - target.getWidth();
        } else if (this.direction == DirectionRight) {
            this.startPositionY = endPositionY;
            this.startPositionX = engine.getCamera().getRight() + 1;
        } else if (this.direction == DirectionTop) {
            this.startPositionX = endPositionX;
            this.startPositionY = engine.getCamera().getTop() + 1;
        } else {
            this.startPositionX = endPositionX;
            this.startPositionY = engine.getCamera().getBottom() + target.getHeight();
        }

        deltaX = endPositionX - startPositionX;
        deltaY = endPositionY - startPositionY;
    }


    @Override
    public IntervalAction reverse() {
        if (fixTargetPosition)
            return new MoveIn(getReverseDirection(direction), duration).targetPosition(endPositionX, endPositionY);
        else
            return new MoveIn(getReverseDirection(direction), duration);
    }

    @Override
    public IntervalAction copy() {
        if (fixTargetPosition)
            return new MoveIn(direction, duration).targetPosition(endPositionX, endPositionY);
        else
            return new MoveIn(direction, duration);
    }
}
